<template>
  <section class="app-inner">
    <el-card class="header-card">
      <el-form :inline="true" :model="queryParams" size="small">
        <el-form-item label="权限名称">
          <el-input clearable placeholder="请输入权限名称" v-model="queryParams.name"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" style="margin-left:30px;">查询</el-button>
          <el-button type="primary" @click="handleSynchro">刷新</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="table-card">
      <section class="table-btn" size="small">
        <el-button @click="handleAdd" type="primary" icon="plus">添加权限</el-button>
      </section>
      <el-table
        border
        :data="tableData"
        style="width: 100%"
        row-key="pkId"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column prop="name" label="权限名称" align="left"></el-table-column>
        <el-table-column prop="frontPath" label="前端组件"></el-table-column>
        <el-table-column prop="url" label="权限URL"></el-table-column>
        <el-table-column prop="menuIcon" label="菜单图标"></el-table-column>
        <el-table-column prop="thdUrl" label="默认跳转地址"></el-table-column>
        <el-table-column prop="isEnable" label="是否启用" :formatter="IsEnableFormatter"></el-table-column>
        <el-table-column prop="level" label="排序" :formatter="analysisLevel"></el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="text" size="small" v-if="scope.row.pid !== 0" @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <upermission-modal ref="UpermissionModal" @ok="modalOk"></upermission-modal>
  </section>
</template>

<script>
import { getJurisdictionTreeList, deleteJurisdiction } from './js/auth'
import { roleIsEnable } from './js/constants'
import UpermissionModal from './components/UpermissionModal'

export default {
  name: 'UpermissionList',
  data () {
    return {
      queryParams: {
        name: ''
      },
      tableData: [],
      menuFolder: [],
      ParentNodeData: []
    }
  },
  components: { UpermissionModal },
  mounted () {
    this.initData()
  },
  methods: {
    setTreeMenu () {
      // 获取子组件值
    },
    IsEnableFormatter (row, column, cellValue) {
      return this.$base.constFormatter(roleIsEnable, Number(cellValue))
    },
    onSubmit () {
      this.initData()
    },
    // 获取权限列表
    async initData () {
      const params = Object.assign({}, this.queryParams)
      let res = await getJurisdictionTreeList(params)
      if (res.code === 0) {
        this.tableData = []
        let treeList = this.generateChildData(res.data)
        for (let a = 0; a < treeList.length; a++) {
          let temp = treeList[a]
          this.tableData.push(temp)
        }
      } else {
        this.$message.warning(res.msg)
      }
    },
    generateChildData (data) {
      const arr = []
      for (var item of data) {
        let menu = {
          id: item.pkId,
          code: item.code,
          isEnable: item.isEnable + '',
          name: item.name,
          frontPath: item.frontPath,
          thdUrl: item.thdUrl,
          menuIcon: item.menuIcon,
          parentId: item.parentId,
          pid: item.parentId,
          pkId: item.pkId,
          level: Number(item.level),
          title: item.name,
          remark: item.remark,
          value: item.pkId,
          url: item.url,
          orderIndex: item.orderIndex
        }
        if (item.children && item.children.length > 0) {
          menu.children = [...this.generateChildData(item.children)]
        }
        arr.push(menu)
      }
      return arr
    },
    // 刷新按钮
    handleSynchro () {
      this.$confirm('将手动同步所有角色信息，是否继续执行？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        this.initData()
      })
    },
    // 删除按钮
    handleDelete (row) {
      this.$confirm('此操作将永久删除该条记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.dataDelete(row)
        })
        .catch(() => {
          this.$message.warning('已取消删除')
        })
    },
    async dataDelete (row) {
      let params = {}
      let res = await deleteJurisdiction(row.pkId, params)
      if (res.code === 0) {
        this.$message.success('删除成功')
        this.initData()
      } else {
        this.$message.warning(res.msg)
      }
    },
    handleDetail () {},
    // 新增按钮
    handleAdd () {
      this.$refs.UpermissionModal.title = '权限新增'
      this.$refs.UpermissionModal.isAdd = true
      this.$refs.UpermissionModal.add()
    },
    // 编辑按钮
    handleEdit (row) {
      this.$refs.UpermissionModal.title = '权限编辑'
      this.$refs.UpermissionModal.isAdd = false
      this.$refs.UpermissionModal.edit(row)
    },
    // 业务弹框回调
    modalOk () {
      this.initData()
    },
    analysisLevel (record) {
      let str = ''
      switch (record.level) {
        case 1:
          str = '一级'
          break
        case 2:
          str = '二级'
          break
        case 3:
          str = '三级'
          break
        case 4:
          str = '四级'
          break
        case 5:
          str = '五级'
          break
        default:
          break
      }
      return str
    },
    // 查询tree
    async getUpermissionTreeTree (row) {
      let params = {}
      let aa
      let res = await getJurisdictionTreeList(params)
      if (res.code === 0 && res.data) {
        this.options = []
        aa = await this.familyTree(res.data, row.parentId)
      } else {
        this.$message.warning(res.msg)
      }
      return aa
    },
    async familyTree (data, id) {
      var temp = {}
      var forFn = function (data, id) {
        for (var i = 0; i < data.length; i++) {
          var item = data[i]
          if (item.pkId === id) {
            temp = item
            forFn(data, item.parentId)
            break
          } else {
            if (item.children) {
              forFn(item.children, id)
            }
          }
        }
      }
      forFn(data, id)
      return temp
    }
  }
}
</script>

<style lang="scss" scoped>
/deep/ .el-table .is-left {
  padding-left: 15px;
  text-align: left !important;
}
.table-btn {
  margin-bottom: 15px;
}
</style>
